package edu.shiep.sunyu;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;

import edu.shiep.sunyu.model.Bus;
import edu.shiep.sunyu.model.BusSchedule;
import edu.shiep.sunyu.model.DBClient;
import edu.shiep.sunyu.model.DBConstants;
import edu.shiep.sunyu.model.Database;
import edu.shiep.sunyu.model.Table;

public class AdminAddTimeToBusLineServlet extends HttpServlet {

	private static final long serialVersionUID = 994471940602074556L;
	
	private DBClient _client = null;
	
	public void init(ServletConfig config) throws ServletException {
	   super.init(config);
	   
	   _client = new DBClient();
	   if (!_client.connect()) {
		   System.out.println("Can't connect to database!!!");
	   }
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// reading the user input
		String bus_id = request.getParameter("bus_id");
		String bus_time_hour = request.getParameter("bus_time_hour");
		String bus_time_min = request.getParameter("bus_time_min");
		
		if (!Utility.isInteger(bus_time_hour) || Integer.parseInt(bus_time_hour) < 0 || Integer.parseInt(bus_time_hour) > 23 ||
			!Utility.isInteger(bus_time_min) || Integer.parseInt(bus_time_min) < 0 || Integer.parseInt(bus_time_min) > 59) {
			request.setAttribute("msg",	"The time format that you input is invalid. Please try again.");
			
			RequestDispatcher view = request.getRequestDispatcher("AdminAddBusLineTime.jsp");
			view.forward(request, response);
			
			return;
		}
		
		String time = bus_time_hour + ":" + bus_time_min + ":00";
		
		Database db = _client.getDB(DBConstants.Const_Database_Name);
		
		Table table = db.getTable(DBConstants.Const_Table_Bus_Name);		
		BasicDBObject query = new BasicDBObject();
		query.put(Bus.Field_Id, bus_id);
		DBCursor cursor = table.query(query);
		if (cursor.size() == 0) {
			request.setAttribute("msg",	"The bus that you input is not exist. Please try again.");
			
			RequestDispatcher view = request.getRequestDispatcher("AdminAddBusLineTime.jsp");
			view.forward(request, response);
			
			return;
		}
		
		table = db.getTable(DBConstants.Const_Table_Bus_Schedule_Name);
		
		query = new BasicDBObject();
		query.put(BusSchedule.Field_Id, bus_id);
		query.put(BusSchedule.Field_Time, time);
		
		cursor = table.query(query);
		if (cursor.size() != 0) {
			// forward back to add bus time page
			// set page error message
			request.setAttribute("msg",	"The schedule is exist. Please try to input another one.");
			
			RequestDispatcher view = request.getRequestDispatcher("AdminAddBusLineTime.jsp");
			view.forward(request, response);
			
			return;
		}
		
		BasicDBObject document = new BasicDBObject();
		document.put(BusSchedule.Field_Id, bus_id);
		document.put(BusSchedule.Field_Time, time);
		
		table.insert(document);
		request.setAttribute("msg",	"The schedule has been added successfully.");
		
		RequestDispatcher view = request.getRequestDispatcher("AdminAddBusLineTime.jsp");
		view.forward(request, response);
	}  
	
}
